<?php

/**
*	Administer Model
*
*	This model is particularly concerned with INSERT and UPDATES to the database since only administrators will need these. There are also
*	SELECT statements included that are specific to functions needed to run the administrative views.
*/
class Administer extends Model {

	/**
	*	Constructor function
	*
	*	Don't really do anything with this, I'm sure i could cut down on redundancy by
	*	loading some stuff here.
	*/
    function Administer()
    {
        // Call the Model constructor
        parent::Model();
    }

	/**
	*	Function: login
	*
	*	Runs a query to verify the login credentials sent to it to access the admin pages.
	*	
	*	@param string $user
	*	@param string $pass
	*
	*	@return array (or FALSE on fail)
	*/
	function login($user,$pass)
	{
		$query = $this->db->query("SELECT u_id, display FROM users WHERE username='$user' AND pass=PASSWORD('$pass')");
		
		if($query->num_rows() == 1) {
        	return $query->row();
		} else {
			return 0;
		}
	}

	/**
	*	Function: admin_home_list
	*
	*	Retrieve recent posts to populate the main admin page, limited by $limit.
	*	
	*	@param integer $limit
	*
	*	@return array
	*/
	function admin_home_list($limit)
	{
		$query = $this->db->query("SELECT posts.p_id, posts.poster, posts.posted, posts.class, posts.title, posts.series, posts.episode, series.s_id, series.name FROM posts LEFT JOIN series ON posts.series=series.s_id ORDER BY posts.p_id DESC LIMIT $limit");
		return $query->result();
	}

	/**
	*	Function: insert_post
	*
	*	Insert a Misc or News post into the database.
	*
	*	$c is for the type (Misc/News)
	*	$t is the post title
	*	$p is the main content of the post
	*	
	*	@param string $user
	*	@param string $c
	*	@param string $t
	*	@param string $p
	*
	*	@return boolean
	*/
	function insert_post($user,$c,$t,$p)
	{
		$query = $this->db->query("INSERT INTO posts (poster, posted, class, title, content, modified) VALUES ('$user', NOW(), '$c', '$t', '$p', NOW())");
		return $query;
	}

	/**
	*	Function: insert_review
	*
	*	Insert a review article into the database.
	*
	*	$c is for the type (Misc/News)
	*	$t is the post title
	*	$p is the main content of the post
	*	$s is the series identifier number
	*	$e is the value used to determine if its a preview, episode review, or complete review.
	*	
	*	@param string $user
	*	@param string $c
	*	@param string $t
	*	@param string $p
	*	@param integer $s
	*	@param integer $e
	*
	*	@return boolean
	*/
	function insert_review($user,$c,$t,$p,$s,$e)
	{
		$query = $this->db->query("INSERT INTO posts (poster, posted, class, title, content, series, episode, modified) VALUES ('$user', NOW(), '$c', '$t', '$p', '$s', '$e', NOW())");
		return $query;
	}
	
	/**
	*	Function: insert_series
	*
	*	Insert a new series into the database along with information about it.
	*
	*	$s is the series title
	*	$st is the animation studio that produced the series
	*	$num is a numeric value used to correctly order the string values of the season
	*	$sea is the string representation of the season the show aired
	*	$y four digit representation of the year in wich the series aired.
	*	$e is the length in episodes of the series
	*	$g is the genre the series falls into
	*	
	*	@param string $s
	*	@param string $st
	*	@param integer $num
	*	@param string $sea
	*	@param integer $y
	*	@param integer $e
	*	@param string $g
	*
	*	@return boolean
	*/
	function insert_series($s,$st,$num,$sea,$y,$e,$g)
	{
		$query = $this->db->query("INSERT INTO series (name, studio, season_num, season, year, length, genre, modified) VALUES ('$s', '$st', '$num', '$sea', '$y', '$e', '$g', NOW())");
		return $query;
	}
	
	/**
	*	Function: get_seriestitle
	*
	*	Used to retrieve the series id and name to be used to populate a dropdown list.
	*
	*	@return array
	*/
	function get_seriestitle()
	{
		$query = $this->db->query("SELECT s_id, name FROM series ORDER BY s_id ASC");
		return $query->result();
	}
	
	/**
	*	Function: edit_list
	*
	*	Used to retrieve the list of entries for the creation of the list of links to edit the items.
	*	The most recent addition is returned last and is the default selection for the dropdown list.
	*	
	*	$cat is used to determine if we will return articles or series.
	*	$offset is used to progress through the entries.
	*
	*	@param string $cat
	*	@param integer $offset
	*
	*	@return array
	*/
	function edit_list($cat,$offset)
	{
		if(strcmp($cat,'post') == 0){
			$query = $this->db->query("SELECT posts.p_id, posts.poster, posts.posted, posts.class, posts.title, posts.series, posts.episode, series.s_id, series.name FROM posts LEFT JOIN series ON posts.series=series.s_id ORDER BY posts.p_id DESC LIMIT $offset, 20");
		} else {
			$query = $this->db->query("SELECT s_id, name FROM series ORDER BY s_id DESC LIMIT $offset, 20");
		}
		return $query->result();
	}
	
	/**
	*	Function: update_review
	*
	*	Updates a given article to be used in editing purposes.
	*
	*	$id is used to limit the update to the specific article to be edited
	*	$c is for the type (Misc/News/Review) if for some reason it needed to be changed
	*	$t is the post title
	*	$p is the main content of the post
	*	$s is the series identifier number
	*	$e is the value used to determine if its a preview, episode review, or complete review.
	*	
	*	@param integer $id
	*	@param string $c
	*	@param string $t
	*	@param string $p
	*	@param integer $s
	*	@param integer $e
	*
	*	@return boolean
	*/
	function update_review($id,$c,$t,$p,$s,$e)
	{
		$query = $this->db->query("UPDATE posts SET class='$c', title='$t', content='$p', series='$s', episode='$e', modified=NOW() WHERE p_id='$id'");
		return $query;
	}
	
	/**
	*	Function: update_post
	*
	*	Used to edit entries that are not reviews. The series and episode values of the table record are nulled out incase 
	*	this was incorrectly added as a review.
	*
	*	$id is used to limit the update to the specific article to be edited
	*	$c is for the type (Misc/News)
	*	$t is the post title
	*	$p is the main content of the post
	*	
	*	@param integer $id
	*	@param string $c
	*	@param string $t
	*	@param string $p
	*
	*	@return boolean
	*/
	function update_post($id,$c,$t,$p)
	{
		$query = $this->db->query("UPDATE posts SET class='$c', title='$t', content='$p', series=NULL, episode=NULL, modified=NOW() WHERE p_id='$id'");
		return $query;
	}
	
	
	/**
	*	Function: insert_series
	*
	*	Update a series entry in the database.
	*
	*	$id is used to limit the update to the specific series to be edited
	*	$s is the series title
	*	$st is the animation studio that produced the series
	*	$num is a numeric value used to correctly order the string values of the season
	*	$sea is the string representation of the season the show aired
	*	$y four digit representation of the year in wich the series aired.
	*	$e is the length in episodes of the series
	*	$g is the genre the series falls into
	*
	*	@param integer $id
	*	@param string $s
	*	@param string $st
	*	@param integer $num
	*	@param string $sea
	*	@param integer $y
	*	@param integer $e
	*	@param string $g
	*
	*	@return boolean
	*/
	function update_series($id,$s,$st,$sn,$sea,$y,$e,$g)
	{
		$query = $this->db->query("UPDATE series SET name='$s', studio='$st', season_num='$sn', season='$sea', year='$y', length='$e', genre='$g', modified=NOW() WHERE s_id='$id'");
		return $query;
	}
	
	
	/**
	*	Function: series_list
	*
	*	This function is used to populate the series list that can be opened when adding a series or editing a review.
	*	Sorted by alphabetical order to aid in the human readability of the list.
	*
	*	@return array
	*/
	function series_list()
	{
		$query = $this->db->query("SELECT s_id, name FROM series ORDER BY name ASC");
		return $query->result();
	}
	
	/**
	*	Function: broadcast_list
	*
	*	This function populates the broadcast schedule page with the shows that are currently flagged as being broadcast.
	*
	*	@return array
	*/
	function broadcast_list()
	{
		$query = $this->db->query("SELECT s_id, name, season, year, broadcast, start FROM series WHERE broadcast=1 ORDER BY start DESC, name ASC");
		return $query->result();
	}
	
	/**
	*	Function: manage_list
	*
	*	This function populates the list in the media management list view of all the recent review type articles.
	*	$offset is used to move through the records.
	*
	*	@param integer $offset
	*
	*	@return array
	*/
	function manage_list($offset)
	{
		$query = $this->db->query("SELECT posts.p_id, posts.poster, posts.posted, posts.class, posts.title, posts.series, posts.episode, series.s_id, series.name FROM posts LEFT JOIN series ON posts.series=series.s_id WHERE posts.class='Review' ORDER BY posts.p_id DESC LIMIT $offset, 20");
		return $query->result();
	}
	
	/**
	*	Function: get_content
	*
	*	This retrieves the post id and main post content to be used with the image insertion & media management capabilities.
	*	$id is used to specify the specific post being requested.
	*
	*	@param integer $id
	*
	*	@return array (or FALSE on failure)
	*/
	function get_content($id)
	{
		$query = $this->db->query("SELECT p_id, content FROM posts WHERE p_id='$id'");
		
		if($query->num_rows() == 1) {
        	return $query->row();
		} else {
			return 0;
		}
	}
	
	/**
	*	Function: update_post_with_images
	*
	*	This function updates the specified post with a new main post content, this is used to insert image blocks for a review
	*	after the article has been submitted and been parsed for image block tags.
	*
	*	$id is used to specify the specific post being requested.
	*	$body is the new content for the review
	*
	*	@param integer $id
	*	@param string $body
	*
	*	@return boolean
	*/
	function update_post_with_images($id,$body)
	{
		$query = $this->db->query("UPDATE posts SET content='$body', modified=NOW() WHERE p_id='$id'");
		return $query;
	}
}
?>